<template>
	<view>
		<!-- 空盒子用来防止消息过少时 拉起键盘会遮盖消息 -->
		<view :animation="anData" style="height:10px;"></view>
		<!--整个对话部分-->
		<scroll-view class="scrollview" scroll-y="true" :scroll-top="scrollTop" style="width: 750rpx;">
			<view class="chat-body">
				<view v-for="(item,index) in messages" :key="index">
					<view class="item self" v-if="item.user=='self'">
						<view class="content-text right">
							{{item.content}}
						</view>
					</view>

					<!--ai回复内容-->
					<view class="item ai">
						<u-image class="avatar" src="/static/user.png" width="25px" height="25px"></u-image>
						<view class="content-text left">
							{{item.content}}
						</view>
					</view>

				</view>

				<view v-for="i in 56">
					{{i}}
				</view>
			</view>
			<view class="input-wraper" :style="{height: `${inputHeight}px`}">
				<view class="input-box" :style="{bottom:`${keyboardHeight}px`}">

					<view class="textarea-style">
						<view class="voice-btn" @click="handleChangeInputType">
							<u-icon name="mic" size="25" v-if="inputType=='voice'" />
							<u-icon custom-prefix="custom-icon-keyboard custom-icon" size="25" v-if="inputType=='text'" />
						</view>

						<view class="out_textarea_box">
							<u-input style="background-color: #FFFFFF;" placeholder="按住 说话"
								@touchstart="handleTouchStart" @touchmove="handleTouchMove"
								@touchend="handleTouchEnd" @blur="scrollToBottom" :adjust-position="false" @focus="focus" />
						</view>
					</view>

				</view>
			</view>

		</scroll-view>


	</view>
</template>

<script>
	export default {
		data() {
			return {
				inputType: 'text',
				inputHeight: 50,
				menuHeight: 0,
				keyboardHeight: 0,
				scrollTop: 0,
				msg: '',
				messages: [{
						user: 'self',
						content: "你好"
					},
					{
						user: "ai",
						content: "你好，请问有什么需要我帮忙的?"
					},
					{
						user: "self",
						content: "一年有多少个月份？"
					},
					{
						user: "ai",
						content: "一年一共有12个月份，分别是:\n一月\n二月 \n三月"
					}
				]
			}
		},
		methods: {
			scrollToBottom() {
				this.keyboardHeight = 0;
				const _this = this;

				setTimeout(() => {
					const query = uni.createSelectorQuery().in(_this);
					query.select('.scrollview').boundingClientRect();
					query.select('.chat-body').boundingClientRect();
					query.exec((res) => {
						if (res[1].height > res[0].height)
							_this.scrollTop = res[1].height - res[0].height
					})
				}, 100);
			},
			focus(e) {
				this.keyboardHeight = e.detail.height;
			},
			handleTouchStart(e) {
				uni.getSetting({
					success(res) {
						if (res.authSetting["scope.record"] === undefined) {
							console.log("第一次授权");
						} else if (!res.authSetting["scope.record"]) {
							uni.showToast({
								icon: "none",
								title: "点击右上角... 进入设置开启麦克风授权",
								duration: 2000
							})
						} else {

						}
					}
				})
			},
			handleChangeInputType(){
				this.inputType=this.inputType=='text'?'voice':'text';
			},
			handlerTouchMove() {

			},
			handleTouchEnd(e) {

			}
		},
	}
</script>

<style lang="scss">
	.scrollview {
		background-color: #FF0000;
	}


	.input-wraper {
		width: 100%;
	}

	.input-box {
		width: 100%;
		min-height: 50px;
		position: fixed;
		bottom: 0;
		background: #e6e6e6;

		.textarea-style {
			width: 100%;
			padding-top: 5px;
			display: flex;

			.out_textarea_box {
				width: 65%;
				min-height: 30px;
				line-height: 30px;
				border-radius: 15px;
				margin-left: 5px;
				overflow: hidden;
				border: 1px solid #333333;
				background: #ffffff;
				display: flex;
				align-items: center;

				textarea {
					width: 100%;
					padding: 0 0%;
					min-height: 30px;
					max-height: 200px;
					font-size: 10px;
					color: #333333;
				}

				input {
					width: 100%;
					text-align: center;
				}

			}

			.voice-btn {
				margin-left: 5px;
				padding: 5px;
				border: 1px solid #FF0000;
			}
		}
	}

	.chat-body {
		background-color: #00FF00;
		width: 100vw;
		height: calc(100vh - 50px);

		.self {
			justify-content: flex-end;
			position: relative;
		}

		.ai {
			position: relative;
		}

		.item {
			display: flex;
			padding: 5px 10px;

			.right {
				background-color: #2b85e4;
			}

			.left {
				background-color: #00FF00;
			}

			.right::after {
				position: absolute;
				display: inline-block;
				content: '';
				width: 0;
				height: 0;
				left: 100%;
				top: 10px;
				border: 12rpx solid transparent;
				border-left: 12px solid #2b85e4;
			}

			.left::after {
				position: absolute;
				display: inline-block;
				content: '';
				width: 0;
				height: 0;
				top: 10px;
				right: 100%;
				border: 12rpx solid transparent;
				border-right: 12rpx solid #FFFFFF;
			}

			.content-text {
				position: relative;
				max-width: 486rpx;
				border-radius: 8rpx;
				word-wrap: break-word;
				padding: 12px 12px;
				margin: 0 12px;
				border-radius: 5px;
				color: #333333;
				line-height: 42rpx;
			}

			.avatar {
				display: flex;
				justify-content: center;
				background: #fff;
				overflow: hidden;

				image {
					align-self: center;
				}
			}

		}
	}
</style>